/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Control;

import DAO.EnderecoDAO;
import DAO.EspacoDAO;
import DAO.RecursoDAO;
import DAO.ReservaDAO;
import DAO.TipoEspacoDAO;
import Model.Endereco;
import Model.Espaco;
import Model.Recurso;
import Model.Reserva;
import Model.TipoEspaco;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;

/**
 *
 * @author Maury
 */
public class pesquisarEspaco extends Processor {

    @Override
    public void execute() throws ServletException, IOException {
        String id = getRequest().getParameter("id");
        String num = getRequest().getParameter("num");
        String end = getRequest().getParameter("end");
        String link = getRequest().getParameter("link");

        if(id == null && num == null && end == null){
            throw new ServletException();
        }
        
        if(link == null){
            throw new ServletException();
        }

        ArrayList<Espaco> espacos = new ArrayList<Espaco>();

        

        if(id != null && !id.equals("")){
            int id_espaco = -1;
            try{
                id_espaco = Integer.parseInt(id);
            }catch(NumberFormatException e){
                String erro = "Id deve ser um número válido!";
                getRequest().setAttribute("erro", erro);
                RequestDispatcher rd = getRequest().getRequestDispatcher("pesquisarEspaco.jsp");
                rd.include(getRequest(), getResponse());                    
            }
            Espaco espaco;
            try {
                espaco = EspacoDAO.PesquisarById(id_espaco);
            } catch (SQLException ex) {
                throw new ServletException(ex);
            } catch (ClassNotFoundException ex) {
                throw new ServletException(ex);
            }
            if(espaco == null){
                String erro = "Espaco não encontrado!";
                getRequest().setAttribute("erro", erro);
                RequestDispatcher rd = getRequest().getRequestDispatcher("pesquisarEspaco.jsp");
                rd.forward(getRequest(), getResponse()); 
            }
            else if(link.equals("removerEspaco.jsp")){
                getRequest().setAttribute("espaco", espaco);
                RequestDispatcher rd = getRequest().getRequestDispatcher(link);
                rd.forward(getRequest(), getResponse());
            } 
                
            else if(link.equals("alterarEspaco.jsp")){
                ArrayList<Endereco> enderecos;
                ArrayList<TipoEspaco> tipos;
                ArrayList<Recurso> recursos;
                try {
                    recursos = RecursoDAO.pesquisarAll();
                    enderecos = EnderecoDAO.pesquisarAll();
                    tipos = TipoEspacoDAO.pesquisarAll();
                } catch (SQLException ex) {
                    throw new ServletException(ex);
                } catch (ClassNotFoundException ex) {
                    throw new ServletException(ex);
                }    
                
                getRequest().setAttribute("recursos", recursos);
                getRequest().setAttribute("enderecos", enderecos);
                getRequest().setAttribute("tipos", tipos);
                getRequest().setAttribute("espaco", espaco);
                RequestDispatcher rd = getRequest().getRequestDispatcher(link);
                rd.forward(getRequest(), getResponse());               
            }
            else if(link.equals("reservarEspaco")){
                ArrayList<Reserva> reservas;
                try {
                    reservas = ReservaDAO.PesquisarByEspaco(id_espaco);
                } catch (SQLException ex) {
                    throw new ServletException(ex);
                } catch (ClassNotFoundException ex) {
                    throw new ServletException(ex);
                }    
                
                getRequest().setAttribute("reservas", reservas);
                getRequest().setAttribute("espaco", espaco);
                RequestDispatcher rd = getRequest().getRequestDispatcher(link);
                rd.forward(getRequest(), getResponse());      
            }
            else{
                throw new ServletException();
            }
        }
        else if(num != null && end != null && !num.equals("") && !end.equals("")){                
            int numero = -1;
            int id_endereco = -1;
            try{
                numero = Integer.parseInt(num);
                id_endereco = Integer.parseInt(end);
            }catch(NumberFormatException e){
                String erro = "Números inválidos";
                getRequest().setAttribute("erro", erro);
                RequestDispatcher rd = getRequest().getRequestDispatcher("pesquisarEspaco.jsp");
                rd.forward(getRequest(), getResponse()); 
            }
            
            Espaco espaco;
            try {
                espaco = EspacoDAO.PesquisarByNumEndereco(numero, id_endereco);
            } catch (SQLException ex) {
                throw new ServletException(ex);
            } catch (ClassNotFoundException ex) {
                throw new ServletException(ex);
            }
            if(espaco == null){
                String erro = "Espaco não encontrado!";
                getRequest().setAttribute("erro", erro);
                RequestDispatcher rd = getRequest().getRequestDispatcher("pesquisarEspaco.jsp");
                rd.include(getRequest(), getResponse()); 
            }
            else if(!link.equals("removerEspaco.jsp")){
                ArrayList<Endereco> enderecos;
                ArrayList<TipoEspaco> tipos;
                ArrayList<Recurso> recursos;
                try {
                    recursos = RecursoDAO.pesquisarAll();
                    enderecos = EnderecoDAO.pesquisarAll();
                    tipos = TipoEspacoDAO.pesquisarAll();
                } catch (SQLException ex) {
                    throw new ServletException(ex);
                } catch (ClassNotFoundException ex) {
                    throw new ServletException(ex);
                }    
                
                getRequest().setAttribute("recursos", recursos);
                getRequest().setAttribute("enderecos", enderecos);
                getRequest().setAttribute("tipos", tipos);
                getRequest().setAttribute("espaco", espaco);
                RequestDispatcher rd = getRequest().getRequestDispatcher(link);
                rd.forward(getRequest(), getResponse());   
            }
            else if(link.equals("removerEspaco.jsp")){
                getRequest().setAttribute("espaco", espaco);
                RequestDispatcher rd = getRequest().getRequestDispatcher(link);
                rd.forward(getRequest(), getResponse());
            }
            else{
                throw new ServletException();
            }
        }
        else if(num != null && !num.equals("")){                
            int numero = -1;
            try{
                numero = Integer.parseInt(num);
            }catch(NumberFormatException e){
                String erro = "Número inválido!";
                getRequest().setAttribute("erro", erro);
                RequestDispatcher rd = getRequest().getRequestDispatcher("pesquisarEspaco.jsp");
                rd.forward(getRequest(), getResponse()); 
            }
            try {
                espacos = EspacoDAO.PesquisarByNum(numero);
            } catch (SQLException ex) {
                throw new ServletException(ex);
            } catch (ClassNotFoundException ex) {
                throw new ServletException(ex);
            }
            if(espacos == null){
                String erro = "Espaco não encontrado!";
                getRequest().setAttribute("erro", erro);
                RequestDispatcher rd = getRequest().getRequestDispatcher("pesquisarEspaco.jsp");
                rd.forward(getRequest(), getResponse()); 
            }
            else{
                getRequest().setAttribute("link", link);
                getRequest().setAttribute("espacos", espacos);
                RequestDispatcher rd = getRequest().getRequestDispatcher("pesquisaEspacoResult.jsp");
                rd.forward(getRequest(), getResponse());
            }
        }
        else if(end != null && !end.equals("")){
            int id_endereco = -1;
            try{
                id_endereco = Integer.parseInt(end);
            }catch(NumberFormatException e){
                String erro = "Números inválidos";
                getRequest().setAttribute("erro", erro);
                RequestDispatcher rd = getRequest().getRequestDispatcher("pesquisarEspaco.jsp");
                rd.forward(getRequest(), getResponse()); 
            }
            try {
                espacos = EspacoDAO.PesquisarByEndereco(id_endereco);
            } catch (SQLException ex) {
                throw new ServletException(ex);
            } catch (ClassNotFoundException ex) {
                throw new ServletException(ex);
            }
            
            if(espacos == null){
                String erro = "Espaco não encontrado!";
                getRequest().setAttribute("erro", erro);
                RequestDispatcher rd = getRequest().getRequestDispatcher("pesquisarEspaco.jsp");
                rd.forward(getRequest(), getResponse()); 
            }
            else{
                getRequest().setAttribute("link", link);
                getRequest().setAttribute("espacos", espacos);
                RequestDispatcher rd = getRequest().getRequestDispatcher("pesquisaEspacoResult.jsp");
                rd.forward(getRequest(), getResponse());
            }
        }
        else{
            try {
                espacos = EspacoDAO.Pesquisar();
            } catch (SQLException ex) {
                throw new ServletException(ex);
            } catch (ClassNotFoundException ex) {
                throw new ServletException(ex);
            }
            
            if(espacos == null){
                String erro = "Espaco não encontrado!";
                getRequest().setAttribute("erro", erro);
                RequestDispatcher rd = getRequest().getRequestDispatcher("pesquisarEspaco.jsp");
                rd.forward(getRequest(), getResponse()); 
            }
            else{
                getRequest().setAttribute("link", link);
                getRequest().setAttribute("espacos", espacos);
                RequestDispatcher rd = getRequest().getRequestDispatcher("pesquisaEspacoResult.jsp");
                rd.forward(getRequest(), getResponse());
            }
        }
    }
    
}
